﻿using AxActUtlTypeLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private AxActUtlTypeLib.AxActUtlType axActUtlType = null;
        public MainWindow()
        {
            InitializeComponent();
            axActUtlType = new AxActUtlTypeLib.AxActUtlType();
            WindowsFormsHost1.Child = axActUtlType;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //连接后，返回的结果
            int iReturnCode;
            //不加这三句会报
            //引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常
            ((System.ComponentModel.ISupportInitialize)(axActUtlType)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(axActUtlType)).EndInit();
            axActUtlType.ActLogicalStationNumber = 1;

            //设置口令
            axActUtlType.ActPassword = "";


            //执行打开口令，并将返回值赋给iReturnCode
            iReturnCode = axActUtlType.Open();
            if (iReturnCode == 0)
            {
                MessageBox.Show("连接PLC成功！");
            }
            else
            {
                MessageBox.Show("连接PLC失败！");
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            int readData = Convert.ToInt16(this.textbox1.Text);
            axActUtlType.WriteDeviceBlock(this.textbox2.Text, 1, ref readData);

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            int iReturnCode;
            short[] arrDviceValue = new short[3];
            arrDviceValue[0] = 1;
            arrDviceValue[1] = 2;
            arrDviceValue[2] = 3;
            iReturnCode = axActUtlType.WriteDeviceRandom2("D7001\nD7002\nD7003", 3, ref arrDviceValue[0]);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            int readData = 0;
            axActUtlType.ReadDeviceBlock(this.textbox2.Text, 1, out readData);
            MessageBox.Show(readData.ToString());
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            short[] arrDceiceValue = new short[3];
            axActUtlType.ReadDeviceRandom2("D7001\nD7002\nD7003", 3, out arrDceiceValue[0]);
            MessageBox.Show(arrDceiceValue[0] + "|" + arrDceiceValue[1] + "|" + arrDceiceValue[2]);
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            int readData = 0;
            axActUtlType.EntryDeviceStatus(this.textbox2.Text, 1, 5, ref readData);
            axActUtlType.OnDeviceStatus += new AxActUtlTypeLib._IActUtlTypeEvents_OnDeviceStatusEventHandler(PlcSocket_OnDeviceStatus);
        }

        public void PlcSocket_OnDeviceStatus(object sender, _IActUtlTypeEvents_OnDeviceStatusEvent e)
        {
            //if (e.szDevice == "D136")
            //{

            //    axActUtlType.FreeDeviceStatus();
            //}
            MessageBox.Show(e.szDevice + "|" + e.lReturnCode + "|" + e.lData);      
        }
    }
}
